﻿using System;
using System.Collections.Generic;
using System.Text;

namespace iFinity.DNN.Modules.CatalookUrlProvider.Entities
{
    internal enum UrlSource
    {
        Name, Description, MetaTitle
    }
    internal class CatalookUrlOptions
    {
        private int _startingProductId;
        private UrlSource _productUrlSource = UrlSource.Name;
        private UrlSource _categoryUrlSource = UrlSource.Name;
        private UrlSource _advCatUrlSource = UrlSource.Name;
        private string _primaryCultureCode;
        private int _tabId;
        private bool _hideListParm;
        private int _listParmToHide;
        private bool _productIdInUrl;
        internal CatalookUrlOptions(int tabId, int startingProductId, string rawProductUrlSource, string rawCategoryUrlSource, string rawAdvCatUrlSource, string primaryCultureCode, bool hideListParm, int listParmToHide, bool productIdInUrl)
        {
            _tabId = tabId;
            _startingProductId = startingProductId;
            _productUrlSource = (UrlSource)EnumConversion.ConvertFromEnum(typeof(UrlSource), rawProductUrlSource, UrlSource.Name);
            _categoryUrlSource = (UrlSource)EnumConversion.ConvertFromEnum(typeof(UrlSource), rawCategoryUrlSource, UrlSource.Name);
            _advCatUrlSource = (UrlSource)EnumConversion.ConvertFromEnum(typeof(UrlSource), rawAdvCatUrlSource, UrlSource.Name);
            _hideListParm = hideListParm;
            _listParmToHide = listParmToHide;
            _primaryCultureCode = primaryCultureCode;
            _productIdInUrl = productIdInUrl;
        }
        internal int StartingProductId 
        { 
            get { return _startingProductId; } 
            set { _startingProductId = value; } 
        }
        internal UrlSource ProductUrlSource
        {
            get { return _productUrlSource; }
            set { _productUrlSource = value; }
        }
        internal UrlSource CategoryUrlSource
        {
            get { return _categoryUrlSource; }
            set { _categoryUrlSource = value; }
        }
        internal UrlSource AdvCatUrlSource
        {
            get { return _advCatUrlSource; }
            set { _advCatUrlSource = value; }
        }
        internal bool UseProductMetaTitles
        {
            get {
                if (_productUrlSource == UrlSource.MetaTitle)
                    return true;
                else
                    return false;
            }
        }
        internal string PrimaryCultureCode
        {
            get { return _primaryCultureCode; }
            set { _primaryCultureCode = value; }
        }
        internal int TabId
        {
            get { return _tabId; }
            set { _tabId = value; }
        }
        internal bool HideListParm
        {
            get { return _hideListParm; }
            set { _hideListParm = value; }
        }
        internal int ListParmToHide
        {
            get { return _listParmToHide; }
            set { _listParmToHide = value; }
        }

        internal bool ProductIdInUrl 
        {
            get { return _productIdInUrl; }
            set { _productIdInUrl = value; }
        }
    }

    internal static class EnumConversion
    {
        internal static Enum ConvertFromEnum(Type enumType, string rawValue, Enum defaultValue)
        {
            if (rawValue != null)
            {
                Enum result = defaultValue;
                try
                {
                    object enumVal = Enum.Parse(enumType, rawValue);
                    result = (Enum)Convert.ChangeType(enumVal, enumType);
                }
                catch (Exception ex)
                {
                    //ignore because we can't implement Enum.TryParse until .NET 4
                }
                return result;
            }
            else
                return defaultValue;

        }
        internal static List<string> ReturnEnumAsList(Type enumType)
        {
            List<string> values = new List<string>();
            foreach (Enum enumVal in Enum.GetValues(enumType))
                values.Add(enumVal.ToString());

            return values;
        }
    }
}
